/**
 * 商品活动查询
 */
var goodsActivitysMask = new Ext.LoadMask(Ext.getBody(), {
			msg : "正在提交,请稍后..."
		});
var queryGoodsActivitySearch = function() {
	var queryGoodsActivityFieldSet = new Ext.form.FieldSet({
		id : 'queryGoodsActivityFieldSet',
		title : '查询条件',
		collapsible : true,// 可折叠
		buttonAlign : 'center',
		keys : [{
					key : 13, // 13代表回车
					fn : queryGoodsActivity,
					scope : this
				}],
		items : [{
			layout : 'column',
			border : false,
			items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '活动名称',
									name : "activityName"
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									anchor : '100%',
									fieldLabel : '规则组规则',
									hiddenName : 'ruleGroupId',
									xtype : 'ruleGroupCmb',
									allowBlank : true,
									showValue : true,
									existall : true
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : '是否可用',
									editable:false,
									hiddenName : "isEnable",
									loaclarr : [['0', '可用'], ['1', '不可用'],
											['', '全部']],
									value : 0
								}]
					}, {
						layout : 'form',
						border : false,
						items : [{
							layout : 'column',
							border : false,
							items : [{
										text : '查询',
										xtype : 'button',
										width : 80,
										handler : function() {
											queryGoodsActivity();
										},
										style : {
											marginLeft : "100px",
											marginRight : "10px"
										}
									}, {
										text : '重置',
										xtype : 'button',
										width : 80,
										handler : function() {
											Ext
													.getCmp('queryGoodsActivityForm')
													.getForm().reset();
										}
									}]
						}]
					}]
		}]
	});
	fieldSetListeners(['queryGoodsActivityFieldSet', 'queryGoodsActivityForm',
					'goodsActivityManagerGrid'], [0.12, 0.88]);

	var queryGoodsActivityForm = new Ext.form.FormPanel({
				id : 'queryGoodsActivityForm',
				anchor : '100% 12%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryGoodsActivityFieldSet]
			});
	return queryGoodsActivityForm;
};

/**
 * grids
 */
var goodsActivityManagerGrid = function() {
	var goodsActivityManagerSm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	// 定义表的每列（对应表头信息）
	var goodsActivityManagerCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				dataIndex : "id",
				header : "商品活动编号",
				width : 80
			}, {
				dataIndex : "activityName",
				header : "活动名称",
				sortable : true,
				width : 80
			}, {
				dataIndex : "ruleGroupName",
				header : "规则组名称",
				sortable : true,
				width : 80
			}, {
				dataIndex : "activityBeginDate",
				header : "活动开始时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "activityEndDate",
				header : "活动结束时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "enjoyOtherActivtity",
				header : "是否参与其它活动",
				sortable : true,
				width : 110,
				renderer : function(value) {
					if (value == 1) {
						return '<font color=\"#FF0000\" >'
								+ PmsUtil.EnjoyOtherActivtity(value)
								+ '</font>';
					}
					return PmsUtil.EnjoyOtherActivtity(value);
				}
			}, {
				dataIndex : "isEnable",
				header : "是否可用",
				sortable : true,
				width : 70,
				renderer : function(value) {
					return PmsUtil.FixingIsEnable(value);
				}
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "createdName",
				header : "创建人",
				sortable : true,
				width : 60
			}, {
				dataIndex : "updatedDate",
				header : "修改时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "updatedName",
				header : "修改人",
				sortable : true,
				width : 60
			}, {
				header : "管理",
				width : 100,
				dataIndex : "id",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var id = g.data.id;
					var isEnable = g.data.isEnable;
					var k = "";
					if (isGranted("goodsActivity_编辑")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updGoodsActivity('
								+ id + ')\">编辑</a>';
					}
					return k;
				}
			}]);

	// 定义分析器
	var goodsActivityManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "ruleGroupId"
						}, {
							name : "activityName"
						}, {
							name : "activityBeginDate"
						}, {
							name : "activityEndDate"
						}, {
							name : "enjoyOtherActivtity"
						}, {
							name : "isEnable"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}, {
							name : "updatedDate"
						}, {
							name : "updatedName"
						}, {
							name : "ruleGroupName"
						}]
			});

	// 取数据store
	var goodsActivityViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/goodsManager/selectGoodsActivity"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryGoodsActivityForm')
											.getForm().getValues());
						}
					}
				},
				reader : goodsActivityManagerReader
			});

	// 定义grid的toolbar
	var goodsActivityTb = new Ext.Toolbar({
				id : "goodsActivityTb",
				height : 25,
				items : []
			});
	goodsActivityTb.add(new Ext.scm.Button({
				text : "添加商品活动",
				iconCls : "btn-add",
				width : 80,
				hsValue : 'goodsActivity_添加',
				pressed : true,
				listeners : {
					"click" : function() {
						addGoodsActivity();
					}
				}
			}));
	goodsActivityTb.add(new Ext.Toolbar.Spacer());
	goodsActivityTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'goodsActivity_刷新',
				pressed : true,
				handler : function() {
					queryGoodsActivity();
				}
			}));

	var goodsActivityManagerGrid = new Ext.grid.GridPanel({
				id : 'goodsActivityManagerGrid',
				store : goodsActivityViewStore,
				cm : goodsActivityManagerCm,
				sm : goodsActivityManagerSm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 88%',
				frame : true,
				tbar : goodsActivityTb,
				bbar : new PG.PagingBar({
							store : goodsActivityViewStore
						})
			});
	return goodsActivityManagerGrid;
};

// 查询
function queryGoodsActivity() {
	var grid = Ext.getCmp("goodsActivityManagerGrid");
	var form = Ext.getCmp("queryGoodsActivityForm");
	if (form.getForm().isValid()) {
		grid.getStore().load();
	}
}

// 添加商品活动
function addGoodsActivity() {
	try {
		addOrUpdGoodsActivity(null, "添加商品活动").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/avtivity/goodsActivityForm.js'],
				function() {
					addOrUpdGoodsActivityWin(null, "添加商品活动").show();
				});
	}
}

// 修改商品活动对象
function updGoodsActivity(id) {
	try {
		addOrUpdGoodsActivity(id, "编辑商品活动").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/avtivity/goodsActivityForm.js'],
				function() {
					addOrUpdGoodsActivityWin(id, "编辑商品活动").show();
				});
	}
}
Ext.getCmp("centerTabPanel").getActiveTab().add(queryGoodsActivitySearch(),
		goodsActivityManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();